﻿
//测试脚本


// 2016-11-07 需求：
/*// 业务逻辑说明：
订单实体：
1、订单状态“正式”，“激活”按钮为不可用；
2、订单状态“生效”，“激活”“提交”都不可用；
3、订单状态为“注销”，则“激活”、“提交”、“注销”都不可用。

// 业务信息：
“激活”按钮ID：new.salesorder.activate
“提交”按钮ID：new.salesorder.tijiao
“注销”按钮ID：new.salesorder.zhuxiao

“状态字段” new_status：
“草稿” 100000000
“生效” 100000001
“注销” 100000002
“正式” 100000003 */
// phenixzh 处理：返回true显示，false隐藏
// “激活按钮”：只有“草稿”状态下显示，其他状态均隐藏

//function ActivateBtnControl() {
//    var blRuturn = true;
//    var orderStatus = Xrm.Page.getAttribute("new_status").getValue();
//    if (orderStatus != null && orderStatus != undefined) {
//        if (orderStatus == "100000000") {
//            blRuturn = true;
//        }
//        else {
//            blRuturn = false;
//        }
//    }
//    else {
//        blRuturn = false;
//    }
//    return blRuturn;
//}
////提交，只有系统管理员能看到

//function TijiaoBtnControl() {
//    var blRuturn = false;
//    //权限控制
//    var roleNames = XrmServiceToolkit.Soap.GetCurrentUserRoles();
//    if (roleNames) {
//        for (var i = 0; i < roleNames.length; i++) {
//            var roleName = roleNames[i];
//            if (roleName) {
//                if (roleName === "系统管理员") {
//                    var orderStatus = Xrm.Page.getAttribute("new_status").getValue();
//                    if (orderStatus) {
//                        if (orderStatus == "100000004") {
//                            blRuturn = true;
//                            break;
//                        }
//                        else {
//                            blRuturn = false;
//                        }
//                    }
//                }
//                else {
//                    blRuturn = false;
//                }
//            }
//        }
//    }
//    return blRuturn;
//}
//// “注销按钮”：“草稿”、“生效”和“正式”状态下显示，其他状态隐藏

//function ZhuxiaoBtnControl() {
//    var blRuturn = true;
//    var orderStatus = Xrm.Page.getAttribute("new_status").getValue();
//    if (orderStatus != null && orderStatus != undefined) {
//        if (orderStatus == "100000000"
//            || orderStatus == "100000001"
//            || orderStatus == "100000003"
//            || orderStatus == "100000004") {
//            blRuturn = true;
//        }
//        else {
//            blRuturn = false;
//        }
//    }
//    else {
//        blRuturn = false;
//    }
//    return blRuturn;
//}




function TijiaoBtnControl1() {
    debugger;
    var blRuturn = false;
    var orderStatus = Xrm.Page.getAttribute("new_status").getValue();
    if (orderStatus) {
        if (orderStatus == "100000004") {
            blRuturn = true;
            break;
        }
        else {
            blRuturn = false;
        }
    }
    return blRuturn;
}



//订单生效按钮显示条件
function TakeeffectBtnControl() {
    var risvisiable = false;
    var sisvisiable = false;

    var showRoles = ["系统管理员","系统定制员","ISKY-订单生效"];
    var roleNames = XrmServiceToolkit.Soap.GetCurrentUserRoles();
    if (roleNames && roleNames.length > 0) {
        for (var i = 0; i < roleNames.length; i++) {
            for (var j = 0; j < showRoles.length; j++) {
                if (roleNames[i] == showRoles[j]) {
                    risvisiable = risvisiable || true;
                    break;
                } else {
                    risvisiable = risvisiable || false;
                    continue;
                }
            }
            if (risvisiable) break;
        }
    } else {
        risvisiable = risvisiable || false;
    }

    var orderStatus = Xrm.Page.getAttribute("new_status").getValue();
    if (orderStatus) {
        //正式
        if (orderStatus == "100000004") {
            sisvisiable = sisvisiable || true;
        }
    }
    debugger;
    return (sisvisiable && risvisiable);
}



//审核按钮显示条件
function AduitBtnControl1() {
    var risvisiable = false;
    var sisvisiable = false;

    var showRoles = ["系统管理员", "ISKY-订单审核"];
    var roleNames = XrmServiceToolkit.Soap.GetCurrentUserRoles();
    if (roleNames && roleNames.length > 0) {
        for (var i = 0; i < roleNames.length; i++) {
            for (var j = 0; j < showRoles.length; j++) {
                if (roleNames[i] == showRoles[j]) {
                    risvisiable = risvisiable || true;
                    break;
                } else {
                    risvisiable = risvisiable || false;
                    continue;
                }
            }
            if (risvisiable) break;
        }
    } else {
        risvisiable = risvisiable || false;
    }

    var orderStatus = Xrm.Page.getAttribute("new_status").getValue();
    if (orderStatus) {
        if (orderStatus == "100000003" || orderStatus == "100000006") {
            sisvisiable = sisvisiable || true;
        }
    }
    debugger;
    return (sisvisiable && risvisiable);
}

//审核按钮提交
function AduitBtnSave1() {
    debugger;
    var message = confirm("点错审核按钮啦！，请取消，审核按钮在隔壁");
    if (message == true) {
        //区域
        var quyu = Xrm.Page.getAttribute("new_quyu").getValue();
        var orderStatus = Xrm.Page.getAttribute("new_status").getValue();
        if (orderStatus && orderStatus == "100000003" || orderStatus == "100000006") {
            var orderprocess = GetsalesorderapprovalprocessInfo(quyu);
            if (orderprocess == null) {
                Xrm.Utility.alertDialog("未找到符合条件的审批流程,请联系管理员！");
                return;
            }
            var customers = Xrm.Page.getAttribute("customerid").getValue();
            if (customers && customers.length > 0) {
                var customerid = customers[0].id;
                var accountres = XrmServiceToolkit.Soap.Retrieve("account", customerid, ["creditlimit"]);
                var creditlimit = 0
                if (accountres &&
                    accountres.attributes["creditlimit"] &&
                    accountres.attributes["creditlimit"].value) {
                    creditlimit = accountres.attributes["creditlimit"].value;
                } else {
                    Xrm.Utility.alertDialog("此客户已经不存在或信用没有给值！");
                    return;
                }

                var fetchXml = "<fetch distinct='false' mapping='logical' aggregate='true' >" +
                    "  <entity name='new_creconsdetail' >" +
                    "    <attribute name='new_quota' alias='sumquota' aggregate='sum' />" +
                    "    <filter>" +
                    "      <filter type='and' >" +
                    "        <condition attribute='statecode' operator='eq' value='0' />" +
                    "        <condition attribute='statuscode' operator='eq' value='1' />" +
                    "        <condition attribute='new_account' operator='eq' value='" + customerid + "' />" +
                    "      </filter>" +
                    "    </filter>" +
                    "  </entity>" +
                    "</fetch>";
                var totalQuota = 0;
                var retrTotalQuota = XrmServiceToolkit.Soap.Fetch(fetchXml);

                if (retrTotalQuota && retrTotalQuota.length > 0) {
                    if (retrTotalQuota[0].attributes["sumquota"].value)
                        totalQuota = parseFloat(retrTotalQuota[0].attributes["sumquota"].value);
                }
                var zongjine = Xrm.Page.getAttribute("new_zongjine").getValue();
                if ((creditlimit + totalQuota) < 0) {
                    Xrm.Utility.alertDialog("客户剩余信用已经为负值");
                } else if (zongjine > (creditlimit + totalQuota)) {
                    Xrm.Utility.alertDialog("该订单总金额已超出客户剩余信用额度！");
                } else {
                    Xrm.Utility.confirmDialog("客户信用原始额度：" + creditlimit + " 客户剩余用信用额度" + (creditlimit + totalQuota));
                    var entityid = Xrm.Page.data.entity.getId();
                    var history = GetsalesorderapprovalhistoryInfo(entityid);
                    if (history != null) {
                        Xrm.Page.getAttribute("new_status").setValue(100000006);
                        Xrm.Page.getAttribute("new_processnotes").setValue(null);
                        var shu = parseInt(history[0].attributes.new_approvalnode.formattedValue) + 1;
                        Xrm.Page.getAttribute("new_processnotes").setValue("当前审核节点:" + shu);
                    } else {
                        Xrm.Page.getAttribute("new_status").setValue(100000006);
                        Xrm.Page.getAttribute("new_processnotes").setValue(null);
                        Xrm.Page.getAttribute("new_processnotes").setValue("当前审核节点:1");
                    }
                    Xrm.Page.data.save().then(function () {
                        Xrm.Utility.alertDialog("审核成功！");
                    }, function () {
                             //审核失败
                        var shufile = parseInt(history[0].attributes.new_approvalnode.formattedValue) + 1;
                        //节点一审核失败
                        if (shufile == 1) {
                            //正式
                            Xrm.Page.getAttribute("new_status").setValue(100000003);
                        } else {
                            //审核中
                            Xrm.Page.getAttribute("new_status").setValue(100000006);
                        }

                        });

                    //, function () {
                    //    //审核失败
                    //    var shufile = parseInt(history[0].attributes.new_approvalnode.formattedValue) + 1;
                    //    //节点一审核失败
                    //    if (shufile == 1) {
                    //        //正式
                    //        Xrm.Page.getAttribute("new_status").setValue(100000003);
                    //    } else {
                    //        //审核中
                    //        Xrm.Page.getAttribute("new_status").setValue(100000006);
                    //    }
                    //    window.location.reload();
                   
                    //history[0].attributes.new_approvalnode1.formattedValue
                    //Xrm.Utility.confirmDialog("客户信用原始额度：" + creditlimit + " 客户剩余用信用额度" + (creditlimit + totalQuota),
                    //    function () {
                    //        Xrm.Page.data.save().then(function () {
                    //            //审核中
                    //            Xrm.Page.getAttribute("new_status").setValue(100000006);
                    //            Xrm.Utility.alertDialog("审核成功！");
                    //            window.location.reload();
                    //        },
                    //            function () {
                    //                Xrm.Utility.alertDialog("提交失败！");
                    //            });
                    //    },
                    //    function () {
                    //        console.log("cancel");
                    //    });
                }
            }
        } else {
            Xrm.Utility.alertDialog("订单在此状态下不允许提交审核");
            return;
        }
    }

}


//

//function FulfillOrderbtn() {
//    var risvisiable = false;
//    var sisvisiable = false;
//    debugger;
//    var showRoles = ["ISKY-成本专员", "系统管理员", "ISKY-订单审核"];
//    var roleNames = XrmServiceToolkit.Soap.GetCurrentUserRoles();
//    if (roleNames && roleNames.length > 0) {
//        for (var i = 0; i < roleNames.length; i++) {
//            for (var j = 0; j < showRoles.length; j++) {
//                if (roleNames[i] == showRoles[j]) {
//                    risvisiable = risvisiable || true;
//                    break;
//                } else {
//                    risvisiable = risvisiable || false;
//                    continue;
//                }
//            }
//            if (risvisiable) break;
//        }
//    } else {
//        risvisiable = risvisiable || false;
//    }
//    var orderStatus = Xrm.Page.getAttribute("new_status").getValue();
//    if (orderStatus) {
//        if (orderStatus == "100000001") {
//            sisvisiable = sisvisiable || true;
//        }
//    }
//    return (sisvisiable && risvisiable);

//}

function GetsalesorderapprovalhistoryInfo(salesorderid) {
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
        "<entity name='new_salesorderapprovalhistory' > " +
    "   <attribute name='new_approvalnode'/>" +
        "  <attribute name='new_salesorderapprovalhistoryid'/>" +
        "   <order attribute='createdon' descending='true'/>" +
        "   <filter type='and'>" +
        "       <condition attribute='statecode' operator='eq' value='0'/>" +
        "       <condition attribute='new_salesorder' operator='eq' uiname='test1' uitype='salesorder' value='" + salesorderid + "'/>" +
        "   </filter>" +
        " </entity>" +
        " </fetch>";
    var retrTotalQuota = XrmServiceToolkit.Soap.Fetch(fetchXml);
    if (retrTotalQuota && retrTotalQuota.length > 0) {
        return retrTotalQuota;
    }
    return null;
}

function GetsalesorderapprovalprocessInfo(territory) {
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
        "<entity name='new_salesorderapprovalprocess'>" +
        "    <all-attributes />" +
        "    <order attribute='createdon' descending='true'/>" +
        "    <filter type='and'>" +
        "        <condition attribute='statecode' operator='eq' value='0'/>" +
        "        <condition attribute='new_territory' operator='eq' value='" + territory + "'/>" +
        "        <condition attribute='statuscode' operator='eq' value='1' />" +
        "    </filter>" +
        "</entity>" +
        "</fetch>";
    var retrTotalQuota = XrmServiceToolkit.Soap.Fetch(fetchXml);
    if (retrTotalQuota && retrTotalQuota.length > 0) {
        return retrTotalQuota;
    }
    return null;
}